function initCKeditor() {
	var basicToolBar = [ [ 'Source', '-', 'Bold', 'Italic', '-',
			'NumberedList', 'BulletedList', '-', 'Link', 'Unlink', '-', 'About' ] ];

	$('#newsTextArea').ckeditor({
		toolbar : basicToolBar,
		height : '400px',
		width : '700px'
	});

	$('#postTextArea').ckeditor({
		toolbar : 'Full',
		height : '400px',
		width : '700px'
	});
}

function buttonLink() {
	$('.button-link').button();
}

function updateTips(t) {
	tips = $(".validateTips");
	tips.text(t).addClass("ui-state-highlight");
	setTimeout(function() {
		tips.removeClass("ui-state-highlight", 1500);
	}, 500);
}

function isConetentEmpty(element, tips) {
	var elemContent = element.val();
	if (elemContent === undefined || elemContent === "") {
		element.addClass("ui-state-error");
		updateTips(tips);
		return true;
	}
	return false;
}

function showDialog(elem, processSubmit, processCancel, processClose) {
	elem.dialog({
		autoOpen : false,
		height : 700,
		width : 800,
		modal : true,
		buttons : {
			Submit : processSubmit,
			Cancel : processCancel
		},
		close : processClose
	});
}

// Mood Dialog
var moodDialog = function() {
	// a workaround for a flaw in the demo system
	// (http://dev.jqueryui.com/ticket/4375), ignore!
	$("#dialog:ui-dialog").dialog("destroy");

	var moodForm = $("#mood-form");
	var moodContent = $("#moodTextArea"), moodType = 'happy', tips = $(".validateTips");

	function resetAll() {
		$("#moodTextArea").val('');
		$("#moodTextArea").removeClass("ui-state-error");
		$('#mood-form img').removeClass('img-clicked');
	}

	function processSubmit() {
		if (!isConetentEmpty(moodContent, 'Mood Content can not be empty!!!')) {
			submitMoods(moodContent.val(), moodType);
			moodForm.dialog("close");
		}
	}

	function processCancel() {
		moodForm.dialog("close");
	}

	function processClose() {
		resetAll();
	}

	showDialog(moodForm, processSubmit, processCancel, processClose);

	$('#mood-form img').bind({
		click : function(event) {
			event.preventDefault();
			$('#mood-form img').removeClass('img-clicked');
			$(this).addClass('img-clicked');
			moodType = $(this).data('mood');
		},

	});

	$("#new-moods").button().click(function() {
		moodForm.dialog("open");
	});
}

// news dialog
var newsDialog = function() {
	// a workaround for a flaw in the demo system
	// (http://dev.jqueryui.com/ticket/4375), ignore!
	$("#dialog:ui-dialog").dialog("destroy");

	var newsForm = $("#news-form");
	var newsContent = $("#newsTextArea"), tips = $(".validateTips");

	function resetAll() {
		$("#newsTextArea").val('');
		$("#newsTextArea").removeClass("ui-state-error");
	}

	function processSubmit() {
		if (!isConetentEmpty(newsContent, 'News Content can not be empty!!!')) {
			submitNews(newsContent.val());
			newsForm.dialog("close");
		}
	}

	function processCancel() {
		newsForm.dialog("close");
	}

	function processClose() {
		resetAll();
	}

	showDialog(newsForm, processSubmit, processCancel, processClose);

	$("#new-news").button().click(function() {
		newsForm.dialog("open");
	});
}

// post dialog

var postDialog = function() {
	// a workaround for a flaw in the demo system
	// (http://dev.jqueryui.com/ticket/4375), ignore!
	$("#dialog:ui-dialog").dialog("destroy");

	var form = $("#post-form");
	var title = $("#title");
	var content = $("#postTextArea"), tips = $(".validateTips");
	var tags = undefined;

	function resetAll() {
		tags = $("#blog-tags");
		title.val('');
		content.val('');
		if (tags != undefined) {
			tags.val('');
			defaultTag();
		}
		content.removeClass("ui-state-error");
	}

	function processSubmit() {
		if (isConetentEmpty(content, 'Post Content can not be empty!!!')
				|| isConetentEmpty(title, 'Title can not be empty!!!')) {
			return;
		}
		tags = $("#blog-tags");
		var tagsValue = tags.val() || [];
		tagsValue = tagsValue.join(',');
		submitPost(title.val(), content.val(), tagsValue);
		form.dialog("close");
	}

	function processCancel() {
		form.dialog("close");
	}

	function processClose() {
		resetAll();
	}

	showDialog(form, processSubmit, processCancel, processClose);

	$("#new-post").button().click(function() {
		defaultTag();
		form.dialog("open");
	});

	function defaultTag() {
		$("select option").filter(function() {
			return $(this).text() == 'MISC';
		}).attr('selected', true);
	}
}

var editDialog = function() {
	var id = getUrlVars()['blogId'];
	// a workaround for a flaw in the demo system
	// (http://dev.jqueryui.com/ticket/4375), ignore!
	$("#dialog:ui-dialog").dialog("destroy");

	var form = $("#edit-form");
	var title = $("#title");
	var content = $("#postTextArea"), tips = $(".validateTips");

	function resetAll() {
		content.removeClass("ui-state-error");
	}

	function processSubmit() {
		if (isConetentEmpty(content, 'Post Content can not be empty!!!')
				|| isConetentEmpty(title, 'Title can not be empty!!!')) {
			return;
		}
		tags = $("#blog-tags");
		var tagsValue = tags.val() || [];
		tagsValue = tagsValue.join(',');
		updateBlogPost(id, title.val(), content.val(), tagsValue);
		form.dialog("close");
	}

	function processCancel() {
		form.dialog("close");
	}

	function processClose() {
		resetAll();
	}

	showDialog(form, processSubmit, processCancel, processClose);

	$("#edit-post").button().click(function() {
		var dTitle = $('#fetch-blog-post #blog-div .blog-title').text();
		var dContent = $('#fetch-blog-post #blog-div .blog-content').html();
		$("#title").val(dTitle);
		$("#postTextArea").val(dContent);
		form.dialog("open");
	});
}

var tagDialog = function() {
	$("#dialog:ui-dialog").dialog("destroy");

	var form = $("#tag-form");
	var tag = $("#tag");
	var tips = $(".validateTips");

	function resetAll() {
		tag.val('');
	}

	function processSubmit() {
		if (isConetentEmpty(tag, 'Tag Content can not be empty!!!')) {
			return;
		}
		submitTag(tag.val());
		form.dialog("close");
	}

	function processCancel() {
		resetAll();
		form.dialog("close");
	}

	function processClose() {
		resetAll();
		form.dialog("close");
	}

	showDialog(form, processSubmit, processCancel, processClose);

	$("#new-tag").button().click(function() {
		form.dialog("open");
	});
}


var wishDialog = function() {
	$("#dialog:ui-dialog").dialog("destroy");

	var form = $("#wish-form");
	var wish = $("#wish-textarea");
	var tips = $(".validateTips");
	
	function resetAll() {
		$("#wish-textarea").val('');
	}

	function processSubmit() {
		if (isConetentEmpty(wish, 'Wish can not be empty!!!')) {
			return;
		}
		makeWish(wish.val());
		form.dialog("close");
	}

	function processCancel() {
		form.dialog("close");
	}

	function processClose() {
		resetAll();
	}

	showDialog(form, processSubmit, processCancel, processClose);

	$("#make-wish").button().click(function() {
		$("#wish-textarea").val('');
		form.dialog("open");
	});
}

/*
var buildPostPopularity = function() {
	$('#build-pop').bind({
		click : function(event) {
			event.preventDefault();
			buildPostsPopularity('.post-success');
		},
	});
}
*/

/* Commment Dialog */

var submitComment = function() {
	$("#submit-comment").bind({
		click : function(event) {
			event.preventDefault();
			addComment();
		},
	});
}

var resetCommentForm = function() {
	$('#c-text').val('');
	$('#left-characters').text('200');
}

var resetCommentName = function() {
	if ($('#c-name').hasClass('error')) {
		$('#c-name').removeClass('error');
		$('#c-name').val('');
	}
}
var resetCommentEmail = function() {
	if ($('#c-email').hasClass('error')) {
		$('#c-email').removeClass('error');
		$('#c-email').val('');
	}
}
var resetCommentContent = function() {
	if ($('#c-text').hasClass('error')) {
		$('#c-text').removeClass('error');
		$('#c-text').val('');
	}
}

var canEnterCommentText = function() {
	
	
	var textArea = $("#c-text");
	var leftChar = $("#left-characters");
	
	if (leftChar === undefined) {
		return;
	}
	
	var maxLength = leftChar.text();
	
	textArea.bind("keyup change", function() {
		if (leftChar != undefined && leftChar.size() > 0) {
			checkMaxLength(textArea, maxLength);
		}
	})
}

function checkMaxLength(textArea, maxLength) {
	currentLengthInTextarea = textArea.val().length;
	$("#left-characters").text(parseInt(maxLength) - parseInt(currentLengthInTextarea));

	if (currentLengthInTextarea > (maxLength)) {
		// Trim the field current length over the maxlength.
		$('#c-text').val($('#c-text').val().slice(0, maxLength));
		$("#left-characters").text(0);
	}
}


//MISC
var noTwitter = function() {
	alert("Oops! I am out, I don't have Twitter yet!!!");
}